using Senparc.Weixin.Exceptions;
using Senparc.Weixin.Open.Entities.Request;
using Senparc.Weixin.XmlUtility;
using System;
using System.IO;
using System.Xml.Linq;
using Tencent;
namespace Senparc.Weixin.Open.MessageHandlers
{
	public abstract class ThirdPartyMessageHandler
	{
		private PostModel _postModel;
		public XDocument EcryptRequestDocument
		{
			get;
			set;
		}
		public XDocument RequestDocument
		{
			get;
			set;
		}
		public IRequestMessageBase RequestMessage
		{
			get;
			set;
		}
		public string ResponseMessageText
		{
			get;
			set;
		}
		public bool CancelExcute
		{
			get;
			set;
		}
		public ThirdPartyMessageHandler(Stream inputStream, PostModel postModel = null)
		{
			this._postModel = postModel;
			this.EcryptRequestDocument = XmlUtility.XmlUtility.Convert(inputStream);
			this.Init();
		}
		public ThirdPartyMessageHandler(XDocument ecryptRequestDocument, PostModel postModel = null)
		{
			this._postModel = postModel;
			this.EcryptRequestDocument = ecryptRequestDocument;
			this.Init();
		}
		public XDocument Init()
		{
			string sPostData = this.EcryptRequestDocument.ToString();
			WXBizMsgCrypt wXBizMsgCrypt = new WXBizMsgCrypt(this._postModel.Token, this._postModel.EncodingAESKey, this._postModel.AppId);
			string text = null;
			int num = wXBizMsgCrypt.DecryptMsg(this._postModel.Msg_Signature, this._postModel.Timestamp, this._postModel.Nonce, sPostData, ref text);
			XDocument result;
			if (num != 0)
			{
				this.CancelExcute = true;
				result = null;
			}
			else
			{
				this.RequestDocument = XDocument.Parse(text);
				this.RequestMessage = RequestMessageFactory.GetRequestEntity(this.RequestDocument, null);
				result = this.RequestDocument;
			}
			return result;
		}
		public void Execute()
		{
			if (!this.CancelExcute)
			{
				this.OnExecuting();
				if (!this.CancelExcute)
				{
					try
					{
						if (this.RequestMessage != null)
						{
							switch (this.RequestMessage.InfoType)
							{
							case RequestInfoType.component_verify_ticket:
							{
								RequestMessageComponentVerifyTicket requestMessage = this.RequestMessage as RequestMessageComponentVerifyTicket;
								this.ResponseMessageText = this.OnComponentVerifyTicketRequest(requestMessage);
								break;
							}
							case RequestInfoType.unauthorized:
							{
								RequestMessageUnauthorized requestMessage2 = this.RequestMessage as RequestMessageUnauthorized;
								this.ResponseMessageText = this.OnUnauthorizedRequest(requestMessage2);
								break;
							}
							default:
								throw new UnknownRequestMsgTypeException("未知的InfoType请求类型", null);
							}
						}
					}
					catch (Exception ex)
					{
						throw new MessageHandlerException("ThirdPartyMessageHandler中Execute()过程发生错误：" + ex.Message, ex);
					}
					finally
					{
						this.OnExecuted();
					}
				}
			}
		}
		public virtual void OnExecuting()
		{
		}
		public virtual void OnExecuted()
		{
		}
		public virtual string OnComponentVerifyTicketRequest(RequestMessageComponentVerifyTicket requestMessage)
		{
			return "success";
		}
		public virtual string OnUnauthorizedRequest(RequestMessageUnauthorized requestMessage)
		{
			return "success";
		}
	}
}
